package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1、构建分页对象
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新建套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //因为传来的setmealDto中包含新建的套餐信息，还有套餐和菜品的关系，所以必须分开保存
        //1、新建套餐对象，让其保存
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(0);
        setmealMapper.insert(setmeal);
        //因为新插入的套餐数据拿不到他的主键id，所以必须得把它接回来
        Long setmealId = setmeal.getId();

        //2、向套餐和菜品关系表中插入n条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //因为在菜品中插入的数据后才能返回他的主键id，所以在口味表中关联的dishId始终为空，所以现在需要对其进行设置
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {

        SetmealVO setmealVO = new SetmealVO();
        //1、首先是查询套餐表，因为这个需要两个部分组成，一个是套餐，一个是关系
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);

        //2、查关系表
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //1、前端传过来的数据中，包含套餐信息和关系表数据，首先先修改套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //2、修改关系表数据，为了方便，关系的录入，假如添加新的关系，不如直接关系全部删除，在重新添加
        Long setmealId = setmealDTO.getId();
        //先删除
        setmealDishMapper.deleteBySetmealId(setmealId);
        //在添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 修改套餐起售状态
     *
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        if (status == StatusConstant.DISABLE) {
            Setmeal setmeal = Setmeal.builder()
                    .status(status)
                    .id(id)
                    .build();
            setmealMapper.update(setmeal);
        }else {
            List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
            setmealDishes.forEach(setmealDish -> {
                Long dishId = setmealDish.getDishId();
                Dish dish = dishMapper.selectById(dishId);
                if (dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }else {
                    Setmeal setmeal = Setmeal.builder()
                            .status(status)
                            .id(id)
                            .build();
                    setmealMapper.update(setmeal);
                }
            });
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        //1、判断当前套餐是否能够删除--是否存在起售中？
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //2、删除套餐表中的套餐数据
        for (Long id : ids) {
            setmealMapper.deleteById(id);
            //4、删除套餐关联的套餐菜品关系表表数据
            setmealDishMapper.deleteBySetmealId(id);
        }
    }
}
